// pages/resume-pages/job-expect/job-expect.js
const app = getApp();
import Toast from '../../../component/toast/toast';
const salaryArr = [25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100, 105, 110, 115, 120, 125, 130, 135, 140, 145, 150, 155, 160, 165, 170, 175, 180, 185, 190, 195, 200, 205, 210, 215, 220, 225, 230, 235, 240, 245, 250];
const monthHours = [
    '5h','10h','15h','20h','25h','30h','35h','40h','45h','50h','55h','60h','65h','70h','75h','80h','85h','90h','95h','100h','105h','110h','115h','120h','125h','130h','135h','140h','145h','150h','155h','160h','165h','170h','175h','180h','185h','190h','195h','200h','205h','210h','215h','220h','225h','230h','235h','240h','245h','250h','255h','260h','265h','270h','275h','280h','285h','290h','295h','300h']
Page({

    /**
     * 页面的初始数据
     */
    data: {
        jobWayObj: null,
        jobType: '',
        skillNameArr: [],
        skills: [],
        salaryArray:salaryArr,
        salaryIndex: [0, 0],
        otherParams: [],
        workHour: '',
        mode: '',
        monthWorkHoursArr: monthHours,
        monthWorkHours:'',
        salaryValue:'',
        selectSalary:null,
        resumeInfoId:''
    },
    onClick(e) {
        let index = e.currentTarget.dataset.index;
        let url = '';
        switch (index) {
            case '1':
                url = '/pages/compony-pages/job-way/job-way?flag=1'
                break;
            case '2':
                if (!this.data.jobWayObj) {
                    Toast("请先选择期望职位");
                    return;
                }
                url = '/pages/compony-pages/compony-select-skill/compony-select-skill?id=' + this.data.jobWayObj.Id;
                break;
        }
        wx.navigateTo({
            url: url
        });
    },
    bindSalaryItemPickerChange(e) {
        // console.log('修改的列为', e.detail.column, '，值为', e.detail.value);
        let data = {
            salaryArray: this.data.salaryArray,
            salaryIndex: this.data.salaryIndex
        }
        if (e.detail.column == 0) {
            let arr = this.getNextSalaryData(e.detail.value);
            data.salaryArray[1] = arr;
            data.salaryIndex[0] = e.detail.value;
            this.setData(data);
        } else if (e.detail.column == 1) {
            data.salaryIndex[1] = e.detail.value;
        }

    },
    bindSalaryPickerChange(e) {
        let that = this;
        that.getOngoingOrderCount(function(){
            that.setData({
                salaryValue: that.data.salaryArray[e.detail.value]
            });
        });
    },
    modePickerChange(e) {
        let that = this;
        this.setData({
            mode: that.data.otherParams.ModeData[e.detail.value]
        });
    },
    workHoursPickerChange(e) {
        let that = this;
        this.setData({
            workHour: that.data.otherParams.WorkingHoursData[e.detail.value]
        });
    },
    monthWorkHoursPickerChange(e){
        let that = this;
        this.setData({
            monthWorkHours: that.data.monthWorkHoursArr[e.detail.value]
        });
    },
    clickSave(){
        if (!this.data.jobWayObj){
            Toast("请选择期望职位");
            return;
        }
        if (!this.data.skills) {
            Toast("请选择技能标签");
            return;
        }
        if (!this.data.skills) {
            Toast("请选择技能标签");
            return;
        }
        if (!this.data.salaryValue){
            Toast("请选择期望薪资");
            return;
        }
        if (!this.data.monthWorkHours){
            Toast("请选择月工作时长");
            return;
        }
        if (!this.data.workHour) {
            Toast("请选择期望工作时段");
            return;
        }
        if (!this.data.mode) {
            Toast("请选择工作方式");
            return;
        }
        let data = this.joinData();
        this.saveResumeInfoMain(data);
    },
    joinData(){
        let data = {
            "JobPosition": "string",
            "SalaryMin": 0,
            "SalaryMax": 0,
            "WorkTime": 0,
            "WorkingHours": "string",
            "JobMode": "string",
            "Skills": [
                {
                    "Id": "string",
                    "ResumeInfoId": "string",
                    "SkillName": "string",
                    "SortNo": 0
                }
            ]
        };
        data.UserInfoId = app.userInfo.Id;
        data.JobPosition = this.data.jobWayObj.Id;
        data.WorkTime = this.data.monthWorkHours.split('h')[0];
        data.WorkingHours = this.data.workHour.Id;
        data.JobMode = this.data.mode.Id;
        data.Skills = this.data.skills;
        data.HourlySalary = this.data.salaryValue;
        data.MonthlySalary = this.data.salaryValue * data.WorkTime
        console.log("joinData" , data);
        return data;
    },
    //上传信息到服务器
    saveResumeInfoMain(data){
        let that = this;
        wx.showLoading({
            title: '加载中...',
            mask: true
        })
        wx.request({
            url: app.globalData.serverAddress.saveResumeInfoMain,
            header: {
                'Authorization': "Bearer " + app.globalData.token
            },
            data:data,
            method: 'POST',
            success: function (res) {
                console.log("saveResumeInfoMain success：", res.data.BodyContent);
                if (res.statusCode == 200) {
                    if (res.data.CodeStatus == 20000){
                        wx.navigateBack({
                            delta: 1,
                        });
                    }
                }
            },
            fail: function (res) {
                console.log("saveResumeInfoMain fail", res)
            },
            complete: function (res) {
                wx.hideLoading()
            },
        });
    },
    //获取正在执行中的订单数量
    getOngoingOrderCount(func){
        let that = this;
        wx.showLoading({
            title: '加载中...',
            mask: true
        })
        wx.request({
            url: app.globalData.serverAddress.getOngoingOrderCount,
            header: {
                'Authorization': "Bearer " + app.globalData.token
            },
            data: {
                resumeInfoId : that.data.resumeInfoId
            },
            method: 'GET',
            success: function (res) {
                console.log("getOngoingOrderCount success：", res.data.BodyContent);
                if (res.statusCode == 200) {
                    if (res.data.CodeStatus == 20000) {
                        if (res.data.BodyContent <= 0){
                            func();
                        }else{
                            Toast('必须先完成手上的订单后，才能修改时薪哟~')
                        }
                    }
                }
            },
            fail: function (res) {
                console.log("getOngoingOrderCount fail", res)
            },
            complete: function (res) {
                wx.hideLoading()
            },
        });
    },
    /**
     * 薪资范围筛选
     */
    getNextSalaryData(index) {
        let arr = [];
        let startIndex = index + 1;
        let size = startIndex + startIndex;
        if (size >= 10) {
            size = startIndex + 10;
        }
        if (index >= 49) {
            size = 60;
        }
        if (index >= 59) {
            size = startIndex + 5;
        }
        if (index >= salaryArr.length - 6) {
            size = salaryArr.length;
            arr = [];
        }
        if (index >= salaryArr.length - 1) {
            arr.push(salaryArr[salaryArr.length - 1]);
        }
        for (; startIndex < size; startIndex++) {
            arr.push(salaryArr[startIndex]);
        }
        return arr;
    },
    getPositionOtherParams() {
        let that = this;
        wx.showLoading({
            title: '加载中...',
            mask: true
        })
        wx.request({
            url: app.globalData.serverAddress.getPositionOtherParams,
            header: {
                'Authorization': "Bearer " + app.globalData.token
            },
            method: 'GET',
            success: function(res) {
                console.log("getPositionOtherParams success：", res.data.BodyContent);
                if (res.statusCode == 200 && res.data.CodeStatus == 20000) {
                    that.setData({
                        otherParams: res.data.BodyContent
                    });
                }
            },
            fail: function(res) {
                console.log("getPositionOtherParams fail", res)
            },
            complete: function(res) {
                wx.hideLoading()
            },
        });
    },
    onLoad: function(options) {
        this.getPositionOtherParams();
        if (options.parmas){
            let parmas = JSON.parse(options.parmas);
            this.setData(parmas);
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})